package cn.newgrand.netcall.util;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class StringUtil {
	
	public static String getPrimaryKey(){
		String currentTime = String.valueOf(System.currentTimeMillis());
		String randomString = randomGetChar();
		return currentTime + randomString;
	}
	
	public static String randomGetChar(){
		List<String> arrList = Arrays.asList("1", "2", "3", "4", "5", "6", "7",
                "8", "9", "0", "A", "B", "C", "D", "E", "F", "G", "H", "I",
                "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U",
                "V", "W", "X", "Y", "Z", "a", "b", "c", "d", "e", "f", "g", "h", "i",
                "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u",
                "v", "w", "x", "y", "z");
        Random random = new Random();
        StringBuilder builder= new StringBuilder();
        for(int i=0 ; i<6; i++){
            String str=arrList.get(random.nextInt(arrList.size()));
            builder.append(str);
        }
        return builder.toString();
	}
	
	public static String parseLong2Date(Long logtime){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date(logtime);
		return sdf.format(date);
	}
	
	public static String getJidNode(String jid){
		if(jid.contains("@")){
			jid = jid.substring(0,jid.lastIndexOf("@"));
		}
		return jid;
	}
	
	public static String getBaseJid(String jid){
		if(jid.contains("/")){
			jid = jid.substring(0,jid.lastIndexOf("/"));
		}
		return jid;
	}
}
